﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KpiConfigProject.Manager;
using KpiConfigProject.Entity;
using KpiConfigProject.TypeEditor.Controls;

namespace KpiConfigProject.ChildForm
{
    public partial class DrillConConfigForm : Form
    {
        public DrillConConfigForm()
        {
            InitializeComponent();
        }

        private Dictionary<int, List<DataRow>> drillMenuTable;
        private Dictionary<int, DataGridViewRow> menuGridDic;
        private void DrillConConfigForm_Load(object sender, EventArgs e)
        {
            InitAllData();
        }

        private void InitAllData()
        {
            DrillManager drillManager = new DrillManager();
            DataTable table = drillManager.GetList();
            if (table != null)
            {
                dgve_Drill.DataSource = table.DefaultView;
            }

            ContextMenuManager menuManager = new ContextMenuManager();
            table = menuManager.GetList();
            if (table != null)
            {
                DataColumn column = table.Columns.Add("", typeof(bool));
                column.SetOrdinal(0);
                dgve_Menu.DataSource = table.DefaultView;
                dgve_Menu.Columns[0].Width = 20;
                for (int i = 1; i < dgve_Menu.Columns.Count; i++)
                {
                    dgve_Menu.Columns[i].ReadOnly = true;
                }
                menuGridDic = new Dictionary<int, DataGridViewRow>();
                foreach (DataGridViewRow row in dgve_Menu.Rows)
                {
                    menuGridDic.Add(int.Parse(row.Cells[1].Value.ToString()), row);
                }
            }

            DrillMenuManager drillMenuManager = new DrillMenuManager();
            drillMenuTable = drillMenuManager.GetGroupDictionary();
            drillMenuManager = null;

            KpiInfoManager kpiInfoManager = new KpiInfoManager();
            table = kpiInfoManager.GetList();
            if (table != null)
            {
                dgve_NewKpiInfo.DataSource = table.DefaultView;
            }

            table = kpiInfoManager.GetList("select vcKpiName,vcDrillTable,vcDrillCondition,vcDelayFormula from pmc3.dtbKpiInfo");
            if (table != null)
            {
                dgve_OldKpiInfo.DataSource = table.DefaultView;
            }
        }

        private void tsb_Save_Click(object sender, EventArgs e)
        {
            DrillManager manager = new DrillManager();
            bool isSucc = manager.InsertData(dgve_Drill.AddRows, dgve_Drill.ModRows, dgve_Drill.DelRows);
            if (!isSucc)
                return;
            dgve_Drill.ClearRowData();
            dgve_Menu.EndEdit();
            if (dgve_Drill.SelectedCells.Count <= 0)
                return;
            int rowIndex = dgve_Drill.SelectedCells[0].RowIndex;
            DataGridViewRow selectRow = dgve_Drill.Rows[rowIndex];
            int drillId = int.Parse(selectRow.Cells[0].Value.ToString());
            List<int> drillMenuId = new List<int>();
            foreach (DataGridViewRow row in dgve_Menu.Rows)
            {
                object obj = row.Cells[0].Value;
                if (obj.ToString() == "")
                    continue;
                if (bool.Parse(obj.ToString()))
                {
                    int dimId = int.Parse(row.Cells[1].Value.ToString());
                    drillMenuId.Add(dimId);
                }
            }
            DrillMenuManager drillMenuManager = new DrillMenuManager();
            isSucc = drillMenuManager.InsertData(drillId, drillMenuId);
            if (isSucc)
            {                
                drillMenuTable = drillMenuManager.GetGroupDictionary();
                drillMenuManager = null;
                //dgv_KPIGroup.ClearRowData();
                MessageBox.Show("保存数据成功！");
            }
        }

        private void tsb_Add_Click(object sender, EventArgs e)
        {
            dgve_Drill.AddNewRow();
        }

        private void tsb_Delete_Click(object sender, EventArgs e)
        {
            dgve_Drill.DeleteRows();
        }

        private void tsb_Reduction_Click(object sender, EventArgs e)
        {
            dgve_Drill.RestoreRows();
        }

        private void tsb_Refresh_Click(object sender, EventArgs e)
        {
            InitAllData();
        }

        private void dgve_Drill_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex <= -1 || e.ColumnIndex <= -1)
            {               
                return;
            }
            DataGridViewRow selectRow = dgve_Drill.Rows[e.RowIndex];
            DataGridViewColumnCollection columns = dgve_Drill.Columns;
            dgve_Menu.BeginEdit(true);
            dgve_Menu.RestoreAllRows();
            dgve_Menu.EndEdit();
            int drillId = 0;
            if (selectRow.Cells[0].Value.ToString() != "")
               drillId = int.Parse(selectRow.Cells[0].Value.ToString());
            if (drillMenuTable.ContainsKey(drillId))
            {
                List<DataRow> kpiRows = drillMenuTable[drillId];
                foreach (DataRow row in kpiRows)
                {
                    int kpiId = int.Parse(row["intMenuId"].ToString());
                    if (menuGridDic.ContainsKey(kpiId))
                    {
                        menuGridDic[kpiId].Cells[0].Value = true;
                    }
                }
            }
            string kpiInfoId = selectRow.Cells[1].Value.ToString();
            List<DataGridViewRow> rows = dgve_NewKpiInfo.FilterGridData("intKpiInfoId", kpiInfoId);
            if (rows.Count > 0)
            {
                string kpiName = rows[0].Cells["vcKpiName"].Value.ToString();
                dgve_OldKpiInfo.FilterGridData(kpiName);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgve_Drill_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex <= -1 || e.ColumnIndex <= -1)
                return;
            string columnName = dgve_Drill.Columns[e.ColumnIndex].HeaderText;
            DataGridViewRow selectRow = dgve_Drill.Rows[e.RowIndex];
            if (columnName == "vcDrillCondition" || columnName == "vcDetailFormula")
            {
                using (FormulaEditorCtrl frm = new FormulaEditorCtrl())
                {
                    frm.FormulaString = selectRow.Cells[e.ColumnIndex].Value.ToString();
                    frm.ShowDialog();
                    selectRow.Cells[e.ColumnIndex].Value = frm.FormulaString;
                }
            }
        }
    }
}
